//
//  YSNavigationController.m
//  luGongPro
//
//  Created by 王庆学 on 2017/7/31.
//  Copyright © 2017年 王庆学. All rights reserved.
//

#import "YSNavigationController.h"
#import "UIViewController+PopViewController.h"
#import "UIColor+YS.h"

@interface YSNavigationController ()

@property (nonatomic, retain)UIView *hiddenView;

@end

@implementation YSNavigationController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationBar.barTintColor = [UIColor navigationBarColor];
}

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated{
    if (self.viewControllers.count >= 1){
        viewController.hidesBottomBarWhenPushed = YES;
    }
    [super pushViewController:viewController animated:animated];
}

//左箭头按钮返回调用的方法
- (BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPopItem:(nonnull UINavigationItem *)item{
    @weakify(self);
    if ([self.viewControllers count] < [navigationBar.items count]){
        return YES;
    }
    BOOL shouldPop = YES;
    UIViewController *vc = [self topViewController];
    if ([vc respondsToSelector:@selector(shouldPopViewController)]){
        shouldPop = [vc shouldPopViewController];
    }
    
    if (shouldPop){
        dispatch_async(dispatch_get_main_queue(), ^{
            [self popViewControllerAnimated:YES];
        });
    }else{
        for (UIView *subview in [navigationBar subviews]){
            if (subview.alpha < 1){
                [UIView animateWithDuration:.25 animations:^{
                    subview.alpha = 1;
                }];
            }
        }
    }
    return NO;
}

#pragma mark - Make NavigationBar Hidden

- (void)setNavigationBarCouldBeInvisible{
    [self.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
    [self.navigationBar setShadowImage:[UIImage new]];
    [self.navigationBar setTranslucent:YES];
    
    CGRect frame = self.navigationBar.frame;
    self.hiddenView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height + 20)];
    self.hiddenView.backgroundColor = self.navigationBar.barTintColor;
    [self.view insertSubview:self.hiddenView belowSubview:self.navigationBar];
}

- (void)setNavigationBarVisible:(BOOL)visible{
    if (visible) {
        [self.navigationBar setAlpha:1];
        [self setNavigationBarHidden:NO];
        return;
    }
    [UIView animateWithDuration:0.4 animations:^{
        [self.navigationBar setAlpha:0];
    }];
}

- (void)setNavigationBarAlpha:(float)ratio{
    [self.hiddenView setAlpha:ratio];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
